package com.flyoil.petromp.ui.fragment.fragment_home;

import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cnpc.fyimageloader.glide.GlideHelper;
import com.cnpc.fyviewlibrary.view.CircleImageView;
import com.flyoil.petromp.Constant.ConstantNetworkCode;
import com.flyoil.petromp.R;
import com.flyoil.petromp.apiservice.HttpModel;
import com.flyoil.petromp.apiservice.OnObserverRetrofitResetCallBack;
import com.flyoil.petromp.base.BaseEntity;
import com.flyoil.petromp.base.BaseFragment;
import com.flyoil.petromp.entity.entity_me.UserMessageEntity;
import com.flyoil.petromp.ui.activity.activity_me.MyMessageActivity;
import com.flyoil.petromp.ui.activity.activity_me.SeeSignatureActivity;
import com.flyoil.petromp.ui.activity.activity_me.SetupActivity;
import com.flyoil.petromp.ui.activity.activity_me.UserPersonalActivity;
import com.flyoil.petromp.utils.MoccaPreferences;

/**
 * Created by songzhiiyn on 2017/9/27.
 * 我的界面
 */

public class MeFragment extends BaseFragment {
    private CircleImageView img_head;
    private LinearLayout line_signature;
    private LinearLayout line_userPreson;
    private LinearLayout line_userMessage;
    private LinearLayout line_usersetup;
    private TextView tv_departmentName, tv_name;


    @Override
    protected int getContentViewResId() {
        return R.layout.fragment_me;
    }

    @Override
    protected void initViews() {
        super.initViews();
        httpModel = new HttpModel();
        img_head = $(R.id.img_user_fragment_head);
        tv_name = $(R.id.tv_me_fragment_name);
        tv_departmentName = $(R.id.tv_me_fragment_departmentName);
        line_signature = $(R.id.line_user_fragment_signature);
        line_userPreson = $(R.id.line_me_fragment_user_preson);
        line_userMessage = $(R.id.line_me_fragment_user_message);
        line_usersetup = $(R.id.line_me_fragment_user_setup);

    }

    @Override
    public void onResume() {
        super.onResume();
        GlideHelper.showImage(mContext, MoccaPreferences.user_avatar.get(), img_head, R.mipmap.user_head_icon);
    }


    @Override
    protected void initEvents() {
        super.initEvents();
        line_userPreson.setOnClickListener(onClickListener);
        line_signature.setOnClickListener(onClickListener);
        line_userMessage.setOnClickListener(onClickListener);
        line_usersetup.setOnClickListener(onClickListener);
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent();
            switch (view.getId()) {
                case R.id.line_me_fragment_user_preson://个人信息界面
                    intent.setClass(mContext, UserPersonalActivity.class);
                    break;
                case R.id.line_user_fragment_signature://我的签名
                    intent.setClass(mContext, SeeSignatureActivity.class);
                    break;
                case R.id.line_me_fragment_user_message://我的消息
                    intent.setClass(mContext, MyMessageActivity.class);
                    break;
                case R.id.line_me_fragment_user_setup://设置界面
                    intent.setClass(mContext, SetupActivity.class);
                    break;
            }
            startActivity(intent);
        }
    };

    @Override
    protected void initdatas() {
        super.initdatas();
        httpModel.getUserMessage(null, new OnObserverRetrofitResetCallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity model) {
                super.onSuccess(model);
                UserMessageEntity entity = null;
                if (model instanceof UserMessageEntity)
                    entity = (UserMessageEntity) model;
                if (entity != null && entity.getCode() == ConstantNetworkCode.SuccedCode) {
                    if (entity.getData().getCompanyId() != -1) {
                        MoccaPreferences.companyId.put(entity.getData().getCompanyId());
                        MoccaPreferences.userId.put(entity.getData().getUserId());
                    }
                    tv_name.setText(entity.getData().getReallyName());
                    tv_departmentName.setText(entity.getData().getDepartmentName());
                }
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }


}
